package com.salton123.ui.base

import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import android.widget.LinearLayout
import com.hyphenate.easeim.R
import com.salton123.ui.base.title.ITitleBar
import com.salton123.ui.base.title.TitleBarStyle
import com.wuhenzhizao.titlebar.widget.CommonTitleBar

/**
 * @Time:2021/4/19 21:29
 * @Author:wujinsheng
 * @Description:
 */
abstract class YoumeTitleActivity : YoumeBaseActivity(), ITitleBar {
    private lateinit var rootView: LinearLayout
    private lateinit var contentLayout: FrameLayout

    @Volatile
    private var titleLayout: LinearLayout? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //initRootView
        rootView = LinearLayout(this)
        rootView.id = R.id.salton_id_top_layout
        rootView.orientation = LinearLayout.VERTICAL
        rootView.layoutParams = FrameLayout.LayoutParams(
            FrameLayout.LayoutParams.MATCH_PARENT,
            FrameLayout.LayoutParams.MATCH_PARENT)
        //initContentView
        contentLayout = FrameLayout(this)
        contentLayout.id = R.id.salton_id_content_layout
        contentLayout.layoutParams = LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.MATCH_PARENT)
        rootView.addView(contentLayout)
        contentLayout.addView(LayoutInflater.from(this).inflate(layoutId, null, false))
        val titleView = LayoutInflater.from(this)
            .inflate(R.layout.salton_common_title_bar, null, false)
        syncTitleBar(titleView)
        initTitleBar((titleView.findViewById(R.id.commonTitleBar)))
        setContentView(rootView)
    }

    override fun enableTitleBar(): Boolean {
        return true
    }

    private fun syncTitleBar(titleBarView: View?) {
        if (titleBarView == null) {
            if (titleLayout != null) {  //title布局已经初始化过，清空标题布局
                titleLayout!!.removeAllViews()
            }
        } else {
            if (titleLayout == null) {
                titleLayout = LinearLayout(this)
                titleLayout!!.id = R.id.salton_id_title_layout
                titleLayout!!.orientation = LinearLayout.VERTICAL
                rootView.addView(titleLayout, 0)
                titleLayout!!.addView(titleBarView)
                titleLayout!!.layoutParams = LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT)
            } else {
                titleLayout!!.removeAllViews()
                titleLayout!!.addView(titleBarView)
            }
        }
    }

    fun initTitleBar(titleBar: CommonTitleBar) {
        if (TextUtils.isEmpty(titleText)) {
            titleBar.centerTextView?.visibility = View.GONE
        } else {
            titleBar.centerTextView?.visibility = View.VISIBLE
            titleBar.centerTextView?.text = titleText
        }
        when (leftStyle) {
            TitleBarStyle.ICON_TEXT -> {   //icon & text
                titleBar.leftTextView?.text = leftText
                titleBar.leftTextView?.visibility = View.VISIBLE
                leftIcon?.let {
                    titleBar.leftTextView.setCompoundDrawablesRelativeWithIntrinsicBounds(it, 0, 0, 0)
                }
            }
            TitleBarStyle.ICON -> {    //icon
                titleBar.leftTextView?.text = ""
                titleBar.leftTextView?.visibility = View.VISIBLE
                leftIcon?.let {
                    titleBar.leftTextView.setCompoundDrawablesRelativeWithIntrinsicBounds(it, 0, 0, 0)
                }
            }
            else -> {    //hidden
                titleBar.leftTextView?.visibility = View.GONE
            }
        }

        when (rightStyle) {
            TitleBarStyle.TEXT -> {   //icon & text
                titleBar.rightTextView?.text = rightText
                titleBar.rightTextView?.visibility = View.VISIBLE
                rightIcon?.let {
                    titleBar.rightTextView.setCompoundDrawablesRelativeWithIntrinsicBounds(it, 0, 0, 0)
                }
            }
            TitleBarStyle.ICON -> {    //icon
                titleBar.rightTextView?.text = ""
                titleBar.rightTextView?.visibility = View.VISIBLE
                rightIcon?.let {
                    titleBar.rightTextView.setCompoundDrawablesRelativeWithIntrinsicBounds(it, 0, 0, 0)
                }
            }
            else -> {    //hidden
                titleBar.rightTextView?.visibility = View.GONE
            }
        }
        titleBar.setListener { v, action, _ ->
            when (action) {
                CommonTitleBar.ACTION_LEFT_TEXT -> {
                    onLeftClick(v)
                }
                CommonTitleBar.ACTION_RIGHT_TEXT -> {
                    onRightClick(v)
                }
            }
        }
    }
}